package com.os.lambda.chapter8;

import org.junit.Test;

import java.util.stream.IntStream;

public class Chapter8 {

    @Test
    public void macroTest(){
        Macro macro = new Macro();
    }

    @Test
    public void testObserver(){
        Moon moon = new Moon();
        moon.startSpying(new Nasa());
        moon.startSpying(new Aliens());

        moon.land("An asteroid");
        moon.land("Apollo 11");
    }

    /**
     * 使用集合流重构质数计数程序
     */
    public long countPrimes(int upTo){
        return IntStream.range(1, upTo)
                .parallel()
                .filter(this::isPrime)
                .count();
    }

    private boolean isPrime(int number){
        return IntStream.range(2, number)
                .allMatch(x->(number%x) != 0);
    }
}
